package francis.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;
import francis.model.VideoInfo;
import francis.utils.VideoUtil;
import francis.utils.WebTools;

/**
 * 搜索界面
 * 
 * @author francis
 * 
 */
public class SearchActivity extends Activity {

	ListView view;
	List<Map<String, Object>> data;
	List<VideoInfo> vInfos;
	/**
	 * @author hogan
	 */
	Button searchDetailBtn;
	/**
	 * end
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.list);
		((TextView) this.findViewById(R.id.header_text)).setText("2了吧");

		view = (ListView) this.findViewById(R.id.video_list);
		//hogan 2011-11-23
		ListViewOnItemClickListener listViewListener = new ListViewOnItemClickListener();
		view.setOnItemClickListener(listViewListener);
		//end
		data = new ArrayList<Map<String, Object>>();
		/**
		 * 获取数据
		 */
		vInfos = VideoUtil.getVideoInfos("http://api.bilibili.tv/list?type=xml&tid=1");
		for(VideoInfo info : vInfos) {
			
			Bitmap bitmap = WebTools.returnBitMap(info.getPic());
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("url", bitmap);
			map.put("title",info.getTitle());
			map.put("tag", info.getAuthor() + " " + info.getCreate());
			data.add(map);
		}
		
		
		SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.list_item,
				new String[]{"url","title","tag"},
				new int[]{R.id.list_img,R.id.list_title,R.id.list_tag});
		
		adapter.setViewBinder(new ViewBinder() {
			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				
				if(view instanceof ImageView && data instanceof Bitmap) {
					ImageView iv = (ImageView)view;
					iv.setImageBitmap((Bitmap)data);
					return true;
				}
				return false;
			}
		});
		
		view.setAdapter(adapter);
	}
	
	//hogan 2011-11-23
	public class ListViewOnItemClickListener implements OnItemClickListener
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			VideoInfo info = vInfos.get(position);
			Intent intent = new Intent();
			intent.putExtra("aid", info.getAid());
			intent.setClass(SearchActivity.this, VideoDetailActivity.class);
			startActivity(intent);
		}
		
	}
    //end
}
